const router = require("koa-router")();
const Mock = require("mockjs");
const JWT = require("jsonwebtoken");
let user = [
  {
    username: "admin",
    password: "admin",
    row: "admin",
    info: "https://wanx.alicdn.com/wanx/1111111111/text_to_image/9d7d2898bd5442a6987fc1aff07212f9_0.png?x-oss-process=image/watermark,image_d2FueC93YXRlcm1hcmsvcWlhbndlbl93YXRlcm1hcmsucG5n,t_80,g_se,x_30,y_30/format,webp",
  },
  {
    username: "user",
    password: "user",
    row: "user",
    info: "https://wanx.alicdn.com/wanx/1111111111/text_to_image/ae3cda4f61644ff4a88d1590fb0bcb5a_0.png?x-oss-process=image/watermark,image_d2FueC93YXRlcm1hcmsvcWlhbndlbl93YXRlcm1hcmsucG5n,t_80,g_se,x_30,y_30/format,webp",
  },
];

let { list } = Mock.mock({
  "list|500": [
    {
      "id": "@id",
      "num|+1": 1,
      "title": "@ctitle(4)",
      "cs|500-1000": 1,
      "zt|1": ["关闭", "运行中", "已上线", "异常"],
    },
  ],
});

router.post("/screen", (ctx) => {
  let { value } = ctx.request.body;
  if (value == "" || value == "全部") {
    ctx.body = {
      items: list,
    };
  } else {
    let items = list.filter((item) => item.zt.includes(value));
    ctx.body = {
      items: items,
    };
  }
});

router.put("/edit", (ctx) => {
  let { id } = ctx.request.body;
  let items = list.find((item) => item.id == id);
  if (items) {
    Object.assign(items, ctx.request.body);
    ctx.body = {
      code: 200,
      msg: "数据修改成功",
    };
  } else {
    ctx.body = {
      code: 400,
      msg: "数据修改失败",
    };
  }
});

router.put("/add", (ctx) => {
  list.unshift({ ...ctx.request.body, id: Mock.mock("@id") });
  ctx.body = {
    code: 200,
    msg: "数据添加成功",
  };
});

router.delete("/delete", (ctx) => {
  let { id } = ctx.request.query;

  list = list.filter((item) => item.id !== id);
  ctx.body = {
    code: 200,
    msg: "数据删除成功",
  };
});

router.get("/list", (ctx) => {
  ctx.body = {
    code: 200,
    list,
  };
});

router.get("/userList", (ctx) => {
  ctx.body = {
    code: 200,
    userList: user,
  };
});

router.post("/login", (ctx, next) => {
  let { username, password } = ctx.request.body;
  let items = user.find(
    (item) => item.username == username && item.password == password
  );
  if (items) {
    ctx.body = {
      code: 200,
      msg: "用户登录成功",
      user: items.username,
      row: items.row,
      info: items.info,
      token: JWT.sign(items.username, "22erfgefrgf"),
    };
  } else {
    ctx.body = {
      code: 400,
      msg: "用户登录失败",
    };
  }
});
router.post("/sigin", (ctx) => {
  let { username } = ctx.request.body;
  let items = user.find((item) => item.username == username);
  if (items) {
    ctx.body = {
      code: 400,
      msg: "用户注册失败,账号已存在",
    };
  } else {
    user.push({
      ...ctx.request.body,
      row: "admin",
      info: "https://wanx.alicdn.com/wanx/1111111111/text_to_image/5d0e54076b2b494c93fc4e7a4869f55e_0.png?x-oss-process=image/watermark,image_d2FueC93YXRlcm1hcmsvcWlhbndlbl93YXRlcm1hcmsucG5n,t_80,g_se,x_30,y_30/format,webp",
    });
    ctx.body = {
      code: 200,
      msg: "用户注册成功",
    };
  }
});

router.post("/search", (ctx) => {
  let { value } = ctx.request.body;
  if (value !== "") {
    let items = list.filter(
      (item) => item.title.includes(value) || item.id.includes(value)
    );
    ctx.body = {
      code: 200,
      items: items,
    };
  } else {
    ctx.body = {
      items: list,
    };
  }
});

module.exports = router;
